package com.cnswhy.cloud.server.iot.controller;

import com.cnswhy.cloud.server.common.entity.enumeration.OrderPfxEnum;
import com.cnswhy.cloud.server.core.vo.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "物联云创-聚合码")
@Controller
@RequestMapping("/qrcode")
@Slf4j
public class QrCodeController {
    @ResponseBody
    @RequestMapping(value = "/4TngPbhYma.txt", method = RequestMethod.GET)
    /*public ResponseEntity<byte[]> exportLand2ndClassIndex(){
        return exportLand2ndClassIndex();
    }*/
    public ResponseEntity<byte[]> exportLand2ndClassIndex(){

        //导出的文件存储目录
        String fileSavePath = "4TngPbhYma.txt";
        //保存文本文件
        writeToTxt("ca0676ed776cc7277649b21743839f14", fileSavePath);
        //获取文本文件的ResponseEntity
        try{
            ResponseEntity<byte[]> fileByte = buildResponseEntity(new File(fileSavePath));
            return fileByte;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 将数据写入文本文件
     * @param list
     * @param path
     */
    private void writeToTxt(String list, String path) {
        FileOutputStream outSTr = null;
        BufferedOutputStream Buff = null;
        StringBuffer write ;
        try {
            outSTr = new FileOutputStream(new File(path));
            Buff = new BufferedOutputStream(outSTr);
            write = new StringBuffer();
            write.append(list);
            Buff.write(write.toString().getBytes("UTF-8"));
            Buff.flush();
            Buff.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                Buff.close();
                outSTr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //读取文件
    private ResponseEntity<byte[]> buildResponseEntity(File file) throws IOException {
        byte[] body = null;
        //获取文件
        InputStream is = new FileInputStream(file);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        //设置文件类型
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        //设置Http状态码
        HttpStatus statusCode = HttpStatus.OK;
        //返回数据
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }
    /*@ApiOperation(value = "聚合码跳转", httpMethod = "GET")
    @ApiParam(name = "terminalId", value = "终端号")
    @RequestMapping(value = "/{terminalId}", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Object queryStatus(HttpServletRequest request, @PathVariable String terminalId) {
        String userAgent = request.getHeader("user-agent");
        log.info("userAgent:{} terminalId:{}", userAgent, terminalId);
        if(StringUtils.isBlank(terminalId)){
            return RespVo.fail("无效终端号");
        }
        if(terminalId.startsWith(OrderPfxEnum.IOTCL.getCode())){
            log.info("充电线设备，跳转到充电线模块");
        }
        if (userAgent != null && userAgent.contains("MicroMessenger")){
            log.info("微信扫码");
        }else if (userAgent !=null && userAgent.contains("AlipayClient")){
            log.info("支付宝扫码");
        }
        return  new ModelAndView(new RedirectView("http://ioth5.hechuangvip.com/"));
    }
    @RequestMapping(value = "/4TngPbhYma.txt", method = RequestMethod.POST, produces = "application/octet-stream")
    public void queryStatus(HttpServletRequest request) {
        return;
    }*/

}
